from threading import Thread
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys  import Keys
driver:webdriver=None
scrollHeight=None
webHight=None
class seleniumCrawlsTool():
    def __init__(self,url,scrollHeight=300):
        driver:webdriver = webdriver.Chrome()
        driver.get(url)
        self.driver = driver
        js="return document.body.scrollHeight"
        self.webHight = driver.execute_script(js)
    def close(self):
        self.driver.close()
    def sendKeys(self,key):
        self.driver.sendKeys(key)
    def getElement(self,e_type='id',value=None):
        if e_type=='id':
            value=self.driver.find_element_by_id(value)
        elif e_type=='class':
            value=self.driver.find_element_by_class(value)
        elif e_type=='name':
            value=self.driver.find_element_by_name(value)
        return value
    def getElements(self,e_type='id',value=None):
        if e_type=='id':
            value=self.driver.find_elements_by_id(value)
        elif e_type=='class':
            value=self.driver.find_elements_by_class(value)
        return value

    def scrollHeight(self):
        y=100
        while y < self.webHight:  # 模拟人类浏览网页每次滚动300高度，同时让所有元素加载完毕每次停顿1秒
            js = "window.scrollTo(0,{})".format(y)
            driver.execute_script(js)
            y = y + 300
            sleep(1)
    def getElementValue(self,key,element):
        if key=='text':
            element.text
    def getElementByLinkText(self,text):
        return  self.driver.find_element_by_link_text(text)
    def getNextPage(self):
        nextPage=self.driver.find_element_by_link_text('下一页')
        nextPage.click()